﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WXWeb.Admin
{
    public partial class EditMenu : BasePage
    {
        Access access = new Access();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (GetQeuryString("para") == "update")
                {
                    InitData();
                }
                else if (GetQeuryString("para") == "add")
                {
                    this.txtCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (GetQeuryString("para") == "update")
            {
                Model.Menu menu = GetEntity();
                menu.ID = Convert.ToInt32(GetQeuryString("id"));
                access.UpdateMenu(menu);
                AlertAndCloseAndReloadParend("保存成功！", "test");
            }
            else if (GetQeuryString("para") == "add")
            {
                Model.Menu menu = GetEntity();
                access.AddMenu(menu);
                AlertAndCloseAndReloadParend("保存成功！", "test");
            }
        }

        private void InitData()
        {
            string id = GetQeuryString("id");
            DataSet ds = access.GetMenuEntityById(id);
            DataRow dr = ds.Tables[0].Rows[0];
            this.txtMenuName.Text = dr["MenuName"].ToString();
            this.txtMenuIcon.Text = dr["MenuIcon"].ToString();
            this.txtMenuUrl.Text = dr["MenuUrl"].ToString();
            this.txtCreateTime.Text = Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd");
            this.txtUpdateTime.Text = Convert.ToDateTime(dr["UpdateTime"]).ToString("yyyy-MM-dd");
            this.txtSequnce.Text = dr["suqence"].ToString();
        }
        public Model.Menu GetEntity()
        {
            Model.Menu menu = new Model.Menu();
            menu.MenuName = this.txtMenuName.Text;
            menu.MenuIcon = this.txtMenuIcon.Text;
            menu.MenuUrl = this.txtMenuUrl.Text;
            menu.Suqence = Convert.ToInt32(this.txtSequnce.Text);
            menu.CreateTime = Convert.ToDateTime(this.txtCreateTime.Text);
            menu.UpdateTime = Convert.ToDateTime(this.txtUpdateTime.Text);
            menu.ParentID = Convert.ToInt32(GetQeuryString("ParentId"));
            return menu;
        }
    }
}